package com.liu.api;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * INETAddress类的使用
 * @author liuchengjie
 */
public class Api {

    public static void main(String[] args) throws UnknownHostException {
        //获取本机的INETAddress对象
        InetAddress localHost = null;
        try {
            localHost = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        System.out.println(localHost);
        //根据主机名获取INETAddress对象
        InetAddress localhost = InetAddress.getByName("localhost");
        System.out.println(localhost);
        //根据域名获取INETAddress对象，例如baidu.com
        InetAddress byName = InetAddress.getByName("www.baidu.com");
        System.out.println(byName);
        //通过INETAddress方向获取对应的主机名和地址
        String hostAddress = byName.getHostAddress();
        System.out.println(hostAddress);
    }
}
